package suanfa;

public class QuickSumLRArr2 {
    private static int[] dictArr;

    public static void main(String[] args) {
        int[] arr = new int[]{7, 4, 3, 6, 2, 7, 8, 1, 2, 1, 0, 10};
        int sumVal = getSumLR(arr, 1, 1);
        System.out.println(sumVal);
    }

    private static int getSumLR(int[] arr, int L, int R) {
        if (L > R) {
            return 0;
        }
        if (null == dictArr) {
            dictArr = createDict(arr);
        }
        return dictArr[R] - (L > 0 ? dictArr[L - 1] : 0);
    }

    private static int[] createDict(int[] arr) {
        int[] dict = new int[arr.length];
        int sumVal = 0;
        for (int i = 0; i < arr.length; i++) {
            sumVal = sumVal + arr[i];
            dict[i] = sumVal;
        }
        return dict;
    }
}
